package com.wangzeqi.changliaoba.myself;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.base.BaseActivity;
import com.wangzeqi.changliaoba.login.EnterActivity;

/**
 * Created by dllo on 16/3/2.
 */
public class SetAvtivity extends BaseActivity implements View.OnClickListener {
    private Button btn_logout;
    private SwitchButton switch_accept_news, switch_sound, switch_shock, switch_loundspeaker;
    private LinearLayout ll_sound, ll_shock;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private String myID;
    private AudioManager audioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        btn_logout = (Button) findViewById(R.id.btn_logout);
        switch_accept_news = (SwitchButton) findViewById(R.id.switch_accept_news);
        switch_sound = (SwitchButton) findViewById(R.id.switch_sound);
        switch_shock = (SwitchButton) findViewById(R.id.switch_shock);
        switch_loundspeaker = (SwitchButton) findViewById(R.id.switch_loundspeaker);
        ll_sound = (LinearLayout) findViewById(R.id.ll_sound);
        ll_shock = (LinearLayout) findViewById(R.id.ll_shock);
        switch_accept_news.setOnClickListener(this);
        switch_sound.setOnClickListener(this);
        switch_shock.setOnClickListener(this);
        switch_loundspeaker.setOnClickListener(this);
        myID = EMChatManager.getInstance().getCurrentUser();
        btn_logout.setOnClickListener(this);
        if (!TextUtils.isEmpty(EMChatManager.getInstance().getCurrentUser())) {
            btn_logout.setText(getString(R.string.logout) + "(" + EMChatManager.getInstance().getCurrentUser() + ")");
        }

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        //加id区分状态
        boolean switch_accept_news_status = sharedPreferences.getBoolean(myID + "switch_accept_news", false);
        boolean switch_sound_status = sharedPreferences.getBoolean(myID + "switch_sound", false);
        boolean switch_shock_status = sharedPreferences.getBoolean(myID + "switch_shock", false);
        boolean switch_loundspeaker_status = sharedPreferences.getBoolean(myID + "switch_loundspeaker", false);
        //在最开始进来的时候   初始状态
        if (switch_accept_news_status) {
            switch_accept_news.openSwitch();
        } else {
            switch_accept_news.closeSwitch();
            ll_sound.setVisibility(View.GONE);
            ll_shock.setVisibility(View.GONE);
        }

        if (switch_sound_status) {
            switch_sound.openSwitch();
        } else {
            switch_sound.closeSwitch();
        }

        if (switch_shock_status) {
            switch_shock.openSwitch();
        } else {
            switch_shock.closeSwitch();
        }

        if (switch_loundspeaker_status) {
            switch_loundspeaker.openSwitch();
        } else {
            switch_loundspeaker.closeSwitch();
        }

        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public void onClick(View v) {
        editor = sharedPreferences.edit();
        switch (v.getId()) {
            case R.id.switch_accept_news:
                if (switch_accept_news.isSwitchOpen()) {
                    switch_accept_news.closeSwitch();
                    ll_sound.setVisibility(View.GONE);
                    ll_shock.setVisibility(View.GONE);
                    editor.putBoolean(myID + "switch_accept_news", false);
                } else {
                    switch_accept_news.openSwitch();
                    editor.putBoolean(myID + "switch_accept_news", true);
                    ll_sound.setVisibility(View.VISIBLE);
                    ll_shock.setVisibility(View.VISIBLE);
                }
                editor.commit();
                break;
            case R.id.switch_sound:
                if (switch_sound.isSwitchOpen()) {
                    switch_sound.closeSwitch();
                    editor.putBoolean(myID + "switch_sound", false);
                } else {
                    switch_sound.openSwitch();
                    editor.putBoolean(myID + "switch_sound", true);
                }
                editor.commit();
                break;
            case R.id.switch_shock:
                if (switch_shock.isSwitchOpen()) {
                    switch_shock.closeSwitch();
                    editor.putBoolean(myID + "switch_shock", false);

                } else {
                    switch_shock.openSwitch();
                    editor.putBoolean(myID + "switch_shock", true);
                }
                editor.commit();
                break;
            case R.id.switch_loundspeaker:
                if (switch_loundspeaker.isSwitchOpen()) {
                    switch_loundspeaker.closeSwitch();
                    editor.putBoolean(myID + "switch_loundspeaker", false);
                    audioManager.setSpeakerphoneOn(false);
                    audioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
                    setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
                    //把声音设定成Earpiece（听筒）出来，设定为正在通话中
                    audioManager.setMode(AudioManager.MODE_IN_CALL);
                } else {
                    switch_loundspeaker.openSwitch();
                    editor.putBoolean(myID + "switch_loundspeaker", true);
                    audioManager.setSpeakerphoneOn(true);
                }
                editor.commit();
                break;
            case R.id.btn_logout:
                Intent intent = new Intent("com.wangzeqi.changliaoba.FORCE_OFFLINE");
                sendBroadcast(intent);
                break;
        }
    }
}
